<?php
namespace mobile\controllers;

use frontend\models\ContactForm;
use mobile\models\Message;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;
use yii\base\ErrorException;
use yii\helpers\Html;
use common\widgets\file_upload\FileUpload;
use common\models\member\Member;
use common\models\category\Category;
use common\models\articles\Articles;
use common\models\art\Art;
use common\models\meeting\Meeting;
use yii\data\Pagination;
use yii\web\Request;
use common\models\user\QrcodeForm;
use yii\captcha\Captcha;
use common\models\Setting\Setting;

class MessageController extends Controller
{     
	 public $layout = 'nomalpage';
    /**
     * @inheritdoc
     */
    public function actions()
    {
		
	$setting = Setting::findBySql('SELECT value FROM setting where id in(52,53,54)')->asArray()->all();
		$captchaConfig = array(
        'class' => 'zhongkefu\helpers\WanhuCaptcha',
        'height' => $setting[2]['value'],
        'width' => $setting[1]['value'],
        'maxLength' => 4,
        'minLength' => 4,
		'flag' => $setting[0]['value'],
		); 
		
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
			'captcha' => $captchaConfig, 

        ];
    }
    /**
    * 留言列表 zsn 20160103
    */
  public function actionIndex($id){
  		/*    $tid     父栏目id
          $id      当前栏目id
    */
      // 是否跳转到子栏目
	 	if(Category::find()->where(['status'=> 1, 'id'=> $id])->asArray()->one()['parent_id'] == 0){
	 		$tid = $id;
	 		$childcat = Category::find()->where(['status'=> 1, 'parent_id'=> $id])->asArray()->all();
	 		$id = $childcat[0]['id'];
			$parenttitle = Category::find()->where(['status'=> 1, 'id'=> $tid])->asArray()->one()['name'];
      		$parentinfo = Category::find()->where(['status'=> 1, 'id'=> $tid])->asArray()->one();
	 	}else{
	 		$parentid = Category::find()->where(['status'=> 1, 'id'=> $id])->asArray()->one()['parent_id'];
	 		$childcat = Category::find()->where(['status'=> 1, 'parent_id'=> $parentid])->asArray()->all();
	 		$tid = $parentid;
     		$parenttitle = Category::find()->where(['status'=> 1, 'id'=> $parentid])->asArray()->one()['name'];
			$parentinfo = Category::find()->where(['status'=> 1, 'id'=> $parentid])->asArray()->one();
	 	}
    // 判断导航状态
	 	$view = Yii::$app->view;
	 	$view->params['tid'] = $tid; 

	 	$title = Category::find()->where(['status'=> 1, 'id'=> $id])->asArray()->one()['name'];
	 	$ctype = Category::find()->where(['status'=> 1, 'id'=> $id])->asArray()->one()['ctype'];

    
	 	$query = Message::find()->where(['status' => 1]);
		$count = $query->count();
		$page = new Pagination(['totalCount' => $count, 'pageSize' => '4']);
		$articles = $query->offset($page->offset)
		    ->limit($page->limit)
		    ->all();
        $view = Yii::$app->view;  
        $view->params['tid']=$tid;
        return $this->render('index',[
          'tid' => $tid,
          'id' => $id,
      	  'title' => $title,
          'parenttitle'=>$parenttitle,
		      'parentinfo'=>$parentinfo,
      	  'ctype' => $ctype,
          'childcat' => $childcat,
          'articles' => $articles,
          'page' => $page,
        ]);
  }
  /**
    * 留言填写页 zsn 20170105
    */
  public function actionPage($id=63){
        	/*    $tid     父栏目id
          $id      当前栏目id
    */
      // 是否跳转到子栏目
	 	if(Category::find()->where(['status'=> 1, 'id'=> $id])->asArray()->one()['parent_id'] == 0){
	 		$tid = $id;
	 		$childcat = Category::find()->where(['status'=> 1, 'parent_id'=> $id])->asArray()->all();
	 		$id = $childcat[0]['id'];
			$parenttitle = Category::find()->where(['status'=> 1, 'id'=> $tid])->asArray()->one()['name'];
      		$parentinfo = Category::find()->where(['status'=> 1, 'id'=> $tid])->asArray()->one();
	 	}else{
	 		$parentid = Category::find()->where(['status'=> 1, 'id'=> $id])->asArray()->one()['parent_id'];
	 		$childcat = Category::find()->where(['status'=> 1, 'parent_id'=> $parentid])->asArray()->all();
	 		$tid = $parentid;
     		$parenttitle = Category::find()->where(['status'=> 1, 'id'=> $parentid])->asArray()->one()['name'];
			$parentinfo = Category::find()->where(['status'=> 1, 'id'=> $parentid])->asArray()->one();
	 	}

    // 判断导航状态
	 	$view = Yii::$app->view;
	 	$view->params['tid'] = $tid; 

	 	$title = Category::find()->where(['status'=> 1, 'id'=> $id])->asArray()->one()['name'];
	 	$ctype = Category::find()->where(['status'=> 1, 'id'=> $id])->asArray()->one()['ctype'];

    
	 	$query = Articles::find()->where(['category_id'=>$id, 'status' => Articles::STATUS_ACTIVE]);
		$count = $query->count();
		$page = new Pagination(['totalCount' => $count, 'pageSize' => '24']);
		$articles = $query->offset($page->offset)
		    ->limit($page->limit)
		    ->all();
        $view = Yii::$app->view;  
        $view->params['tid']=$tid;
     
        return $this->render('page',[
        
          'tid' => $tid,
          'id' => $id,
      	  'title' => $title,
          'parenttitle'=>$parenttitle,
		      'parentinfo'=>$parentinfo,
      	  'ctype' => $ctype,
          'childcat' => $childcat,
          'articles' => $articles,
          'page' => $page,
        ]);
  }
  /**
    * 留言提交 zsn 20170105
    */
	public function actionSendpage($id=63){
	
		$model = new Message();
		$model->create_time = time();
		$model->ip = Yii::$app->request->userIP;
		$model->user = Yii::$app->request->post()['user'];
		$model->name = Yii::$app->request->post()['name'];
		$model->phone = Yii::$app->request->post()['phone'];
		$model->email = Yii::$app->request->post()['email'];
		$model->address = Yii::$app->request->post()['address'];
		$model->title = Yii::$app->request->post()['title'];
		$model->content = Yii::$app->request->post()['content'];
		$model->verifyCode  = Yii::$app->request->post()['code'];
        if ($model->save()) {
			    echo "<script>alert('提交完成！');history.go(-2);</script>";
        } else {
        	echo "<script>alert('提交失败！');history.go(-1);</script>";
        }

	}

}